﻿/*

Author :Jezer N.Bautista
August 1, 2012

Logic: We can add validsations.
It will then be stored on validateGroups.
If we use the function Validate, 
it will loop through the validateGroups along with the rules

can specify any groupname

syntax:
                global.validation.add("myGroup", "blanks", $('.downarrowdiv'));
                global.validation.validate("myGroup");
*/



$(document).ready(function () {

    var balloon = $('.downarrowdiv');

    var validateGroups = {};


    global.validation = {

        hide: function () {

            balloon.hide();

        },

        add: function (e, groupName, validationType, message) {

            //check if key exists
            if (!(groupName in validateGroups)) {
                validateGroups[groupName] = new Array();
            }

            validateGroups[groupName].push({ e: e, type: validationType, message: message });
        },

        //Validate 
        validate: function (groupName) {

            //show the element
            global.validation.hide();

            var hasError;

            for (var i = 0; i < validateGroups[groupName].length; i++) {

                hasError = global.validation.execute(validateGroups[groupName][i].e,
                                      validateGroups[groupName][i].type,
                                      validateGroups[groupName][i].message);
                if (hasError) {
                    return hasError;
                    //break;
                }


            };

        },
        //executes inside the validate
        execute: function (e, validationType, message) {


            function showBalloon() {

 
                var top =   e.offset().top -
                            balloon.height() -
                            e.height();


                var left = e.offset().left +
                              e.width() -
                              40;
                
                //Offet is buggy so I used css instead.
                balloon.css({
                   top : top + 'px',
                   left : left +'px'
                });

                balloon.text(message);
                balloon.show();

            }

            switch (validationType) {
                case 'blanks':
                    if (e.val() == '') {
                        showBalloon();
                        return true;
                    }
                    break;
                case 'numeric':
                    return true;
                    break;
                case 'email':
                    return true;
                    break;
                default:
                    return false;
            }

        }
    }



});
